package com.example.whatrubbish.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.whatrubbish.mapper.LoginRecordMapper;
import com.example.whatrubbish.model.Admin;
import com.example.whatrubbish.model.LoginRecord;
import com.example.whatrubbish.service.LoginRecordService;
import com.example.whatrubbish.vo.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LoginRecordServiceImpl implements LoginRecordService {

    @Autowired
    LoginRecordMapper loginRecordMapper;

    @Override
    public int insertSelective(Map record, Admin admin) {
        LoginRecord loginRecord = new LoginRecord();
        Long loginAdminId = admin.getAdminId();
        String loginIp = record.get("ip").toString();
        Map detail = (Map) JSON.parse(record.get("ad_info").toString());
        String loginCountry = detail.get("nation").toString();
        String loginRegion = detail.get("province").toString();
        String loginCity = detail.get("city").toString();
        String loginIsp = detail.get("district").toString();
        Date loginTime = new Date();
        loginRecord.setLoginAdminId(loginAdminId);
        loginRecord.setLoginIp(loginIp);
        loginRecord.setLoginCountry(loginCountry);
        loginRecord.setLoginRegion(loginRegion);
        loginRecord.setLoginCity(loginCity);
        loginRecord.setLoginIsp(loginIsp);
        loginRecord.setLoginTime(loginTime);
        return loginRecordMapper.insertSelective(loginRecord);
    }

    @Override
    public LoginRecord selectLastLoginRecord(Long loginAdminId) {
        return loginRecordMapper.selectLastLoginRecord(loginAdminId);
    }

    @Override
    public Long selectAllCountByAdminId(Criteria criteria) {
        return loginRecordMapper.selectAllCountByAdminId(criteria);
    }

    @Override
    public List<LoginRecord> selectAllByAdminId(Criteria criteria) {
        return loginRecordMapper.selectAllByAdminId(criteria);
    }
}
